package com.ryan.mms.privilage.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ryan.dictionary.framework.enums.AjaxReturnEnum;
import com.ryan.framework.action.BaseAction;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.organization.dto.OrganizationDto;
import com.ryan.mms.organization.model.Organization;
import com.ryan.mms.organization.service.OrganizationService;
import com.ryan.mms.position.dto.PositionDto;
import com.ryan.mms.position.model.Position;
import com.ryan.mms.position.service.PositionService;
import com.ryan.mms.privilage.dto.EmployeeDto;
import com.ryan.mms.privilage.model.Employee;
import com.ryan.mms.privilage.service.EmployeeService;

@Component
public class EmployeeAction extends BaseAction {
	private final Logger logger = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = 1L;
	
	private EmployeeDto employeeDto;
	private Page page;
	@Autowired
	private EmployeeService<EmployeeDto,Employee,Long> employeeService;
	@Autowired
	private OrganizationService<OrganizationDto,Organization,Long> organizationService;
	@Autowired
	private PositionService<PositionDto,Position,Long> positionService;
	

	public String list(){
		try{
			List<EmployeeDto> employeeList = employeeService.find(page==null?new Page():page, employeeDto);
			this.getActionContext().put("page", page);
			this.getActionContext().put("employeeList", employeeList);
			if(null==employeeDto){
				employeeDto = new EmployeeDto();
			}
			this.getActionContext().put("employeeDto", employeeDto);
			this.getActionContext().put("allOrganizationList", organizationService.getAll());
			return SUCCESS;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return ERROR;
		}
	}
	
	public String index(){
		try{
			page = new Page();
			employeeDto = new EmployeeDto();
			List<EmployeeDto> employeeList = employeeService.find(page, employeeDto);
			this.getActionContext().put("page", page);
			this.getActionContext().put("employeeList", employeeList);
			this.getActionContext().put("employeeDto", employeeDto);
			List<OrganizationDto> allOrganizationList = organizationService.getAll();
			this.getActionContext().put("allOrganizationList", null==allOrganizationList?new ArrayList<OrganizationDto>():allOrganizationList);
			return SUCCESS;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return ERROR;
		}
	}
	
	public String edit(){
		try{
			employeeDto = employeeService.save(employeeDto);
			ajaxReturnDataDto.setMessage("员工信息已保存！");
			ajaxReturnDataDto.setStatusCode(AjaxReturnEnum.SUCCESS.getKey());
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			ajaxReturnDataDto.setMessage("员工信息保存失败！");
			ajaxReturnDataDto.setStatusCode(AjaxReturnEnum.ERROR.getKey());
		}
		ajaxReturnDataDto.setNavTabId("editEmployeeNavTab");
		this.getActionContext().put("ajaxReturnDataDto", ajaxReturnDataDto);
		return AJAXDONE;
	}
	
	public String view(){
		try{
			Object employeeId = this.getParameters().get("employeeId");
			employeeDto = employeeService.findById(null==employeeId?0L:Long.parseLong(((String [])employeeId)[0]));
			this.getActionContext().put("employeeDto",null==employeeDto?new EmployeeDto():employeeDto);
			List<PositionDto> allPositionList = positionService.getAll();
			this.getActionContext().put("allPositionList", null==allPositionList?new ArrayList<PositionDto>():allPositionList);
			List<OrganizationDto> allOrganizationList = organizationService.getAll();
			this.getActionContext().put("allOrganizationList", null==allOrganizationList?new ArrayList<OrganizationDto>():allOrganizationList);
			return SUCCESS;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return ERROR;
		}
	}
	
	public String delete(){
		try{
			Object employeeId = this.getParameters().get("employeeId");
			employeeService.deleteById(null==employeeId?0L:Long.parseLong(((String [])employeeId)[0]));
			ajaxReturnDataDto.setMessage("员工信息已删除！");
			ajaxReturnDataDto.setStatusCode(AjaxReturnEnum.SUCCESS.getKey());
		}catch(Exception e){
			ajaxReturnDataDto.setMessage("员工信息删除失败！");
			ajaxReturnDataDto.setStatusCode(AjaxReturnEnum.ERROR.getKey());
			logger.error(e.getMessage(), e);
		}
		ajaxReturnDataDto.setNavTabId("employeeListNavTab");
		this.getActionContext().put("ajaxReturnDataDto", ajaxReturnDataDto);
		return AJAXDONE;
	}

	public String loadChangePassword(){
		return SUCCESS;
	}

	public EmployeeDto getEmployeeDto() {
		return employeeDto;
	}

	public void setEmployeeDto(EmployeeDto employeeDto) {
		this.employeeDto = employeeDto;
	}

	public void setOrganizationService(
			OrganizationService<OrganizationDto, Organization, Long> organizationService) {
		this.organizationService = organizationService;
	}

	public void setEmployeeService(
			EmployeeService<EmployeeDto, Employee, Long> employeeService) {
		this.employeeService = employeeService;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Page getPage() {
		return page;
	}

	public void setPositionService(PositionService<PositionDto, Position, Long> positionService) {
		this.positionService = positionService;
	}
}
